#!/usr/bin/env python

#               Copyright 2010 Morris Blackham
#
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


from lxml import etree
import os
import sys
import commands

global path
global ipaddr
global type

#set variables from command line args
(path, ipaddr, type) = sys.argv[1:]


#set vars to GW agent binaries
gwpoa = "/opt/novell/groupwise/agents/bin/gwpoa";
if not os.path.isfile(gwpoa):
	print "GroupWise agents not found - Please install before continuing"
	quit()

gwmta = "/opt/novell/groupwise/agents/bin/gwmta";
if not os.path.isfile(gwmta):
	print "GroupWise agents not found - Please install before continuing"
	quit()


#load config file
configfile = "/etc/opt/fbc/fbc.xml"

try:
	tree = etree.parse(configfile)
	root = tree.getroot()
except:
	print("config file %s file not found" % configfile)


# get directory name for domain or po
name = os.path.basename(os.path.dirname(path))
print "name is %s" % name



def getsubnet():
	global prefix

	subcmd = "ip addr show eth0 | grep inet | awk '{ print substr($2, length($2)-1)}'"
	r = commands.getstatusoutput(subcmd)
	if r[0] != 0:
		print "command failed"
	else:
		prefix = r[1]
		print prefix
	return(prefix)
	

def checkip():
	
# Ping ip address to see if we can add it as a secondary to this box.

	pingcmd = 'ping -c 3 %s' % ipaddr

	ping_result = commands.getstatusoutput(pingcmd)

	if ping_result[0] == 0:

		# Ping succesfull,  but let's see if it's bound 
		# to local machine
		ipgrep = "ip address show | grep %s" % ipaddr
		if commands.getstatusoutput(ipgrep)[0] == 0:
			pass
		else:
			print "Ping succeeded for %s" % ipaddr  
			print "and is not a local address, Can not add as a secondary ip."
			quit()	

	else:
		
		# add secondary IP address
		getsubnet()
		ipadd = 'ip address add %s/%s dev eth0 label eth0.sec' % (ipaddr, prefix)
		ipadd_result = commands.getstatusoutput(ipadd)

		if ipadd_result[0] != 0:
			print "adding IP failed. exiting"
			quit()
		else:
			cfgfile = "/var/fbc/secip.txt"
			if not os.path.isfile(cfgfile):
				filewrite = open(cfgfile, 'w')
				text = ipaddr
				try:
					filewrite.write(text)
				except:
					print "could not create IP file!"
					quit()	
			else:
				print "file exists", cfgfile

	return

def loadgw():

	# get the directory to set a sym link for the restore area

	#try:
	#	restorebase = root.find("offlinepath").text
	#	restorepath = restorebase + name
	#except:
	#	print "offline path is not set in %s.	Can not continue" % configfile
	#print restorepath


	# create the directory if it doesn't exist
	#if not os.path.isdir(restorebase):
	#	os.makedirs(restorebase)


	# delete the symlink as it may be for a 
	# different day's backup,  then recreate it.

	#if os.path.islink(restorepath):
	#	try:
	#		 os.remove(restorepath)
	#	except:
	#		print "Failed to remove symlink to %s", restorepath

	#try:
	#	os.symlink(path,restorepath)
	#except:
	#	print "Creating simlink to %s failed" % path

	
	# set some vars based on if it's a domain or a po
	if type == "domain":
		agent = gwmta
		agentargs = " --home %s -ip %s >& /dev/null" % (restorepath, ipaddr)
	elif type == "po":
		agent = gwpoa
		agentargs = " --home %s -ip %s --noconfig >& /dev/null" % (restorepath, ipaddr)
	else:
		print "Incorrect value for %s" % type


	# build the command to load the agent, then run the command
	agentcmd = "/sbin/startproc -f -t 2 %s %s" % (agent, agentargs)

	print agentcmd 
	#load_result = commands.getstatusoutput(agentcmd)
	#print load_result[0]
	#if load_result != 0:
	#	print "Loading agent failed"

	return

def main():
	checkip()
	loadgw()
	return

main()	
	
